onload = function(){
	$('.merchantDetailBox .hiddenBox').hide();

	if(sessionStorage.categoryid){
		sessionStorage.removeItem('categoryid');
	}
	if(sessionStorage.cityid){
		sessionStorage.removeItem('cityid');
	}
	if(sessionStorage.keyword){
		sessionStorage.removeItem('keyword');
	}
	getMer(1);
	$.ajax({
		type : 'post',
		url : '/api/v1/merchant/category',
		dataType : 'json',
		timeout : 10000,
		success : function (data) {
			var obj = JSON.parse(data.data);
			var infoList = obj.info;
			$('#load1 ul').html('');
			var $ul = $('#load1 ul');
			for (var i in infoList) {
            	$('<li categoryid='+infoList[i]["id"]+'>'+infoList[i]["name"]+'</li>').appendTo($ul);
            }
            $('#load1 ul li').bind('click',function(){
            	$('#wrapper ul').html('');
            	sessionStorage.categoryid = $(this).attr('categoryid');
            	$('.category a').text($(this).text());
            	$('#load1 ul li').unbind('click');
            	$('.merchantsBox .hiddenBox').css('display','none');
            	document.body.style.overflow = 'auto';
            	getMer(1);
            });
        },
    	error : function (xhr,type,errorThrown) {
			alert('未知错误');
		}
	});

	$.ajax({
		type : 'post',
		url : '/api/v1/region/merchantCity',
		dataType : 'json',
		timeout : 10000,
		success : function (data) {
			if(data.code == 200){
				var obj = JSON.parse(data.data);
				var infoList = obj.info;
				$('#load2 ul').html('');
				var $ul = $('#load2 ul');
				for (var i in infoList) {
                	$('<li cityid='+infoList[i]["id"]+'>'+infoList[i]["name"]+'</li>').appendTo($ul);
                }
                $('#load2 ul li').bind('click',function(){
                	$('#wrapper ul').html('');
                	sessionStorage.cityid = $(this).attr('cityid');
                	$('.chooseCity a').text($(this).text());
                	$('#load2 ul li').unbind('click');
                	$('.merchantsBox .hiddenBox').css('display','none');
                	document.body.style.overflow = 'auto';
                	getMer(1);
                });
            }
        },
        error : function (xhr,type,errorThrown){
        	alert('未知错误');
        }
    });
}

$.ajax({
	type : 'post',
	url : '/api/v1/common/getMerchantIndexImages',
	dataType : 'json',
	timeout : 10000,
	success : function (data) {
		if(data.code == 200){
			var obj = JSON.parse(data.data);
			console.log(obj);
			var merchantImages = obj.merchantImages;
			var boxHtml =" ";
			$.each(merchantImages, function(i, item) {
				boxHtml += " <div class='swiper-slide'> <img src='" + item.img + "' style='width:100%;'/></div>";
			});
			$("#swiper-wrapper").html(boxHtml);
			var mySwiper = new Swiper('.swiper-container',{
	            pagination: '.pagination',
	            paginationClickable: true,
	            moveStartThreshold: 100,
	            observer:  true,//修改swiper自己或子元素时，自动初始化swiper
	            observeParents: true,//修改swiper的父元素时，自动初始化swiper
	            autoplay : 3000,
	            loop : true,
		        speed : 500
			});
			var $width = $('.pagination').width();
			$('.pagination').css('margin-left',-$width/2);
		}
	},
	error : function (xhr,type,errorThrown) {
		// alert('未知错误');
	}
});

var myScroll1,myScroll2;
document.body.style.overflow = 'hidden';

myScroll1 = new IScroll('#load1', { 
	probeType: 3, 
	mouseWheel: true,
	bounce: false,
	click:true
});

myScroll2 = new IScroll('#load2', { 
	probeType: 3, 
	mouseWheel: true,
	bounce: false,
	click:true
});

$('input').focus(function(){
	this.select();
});

function getMer(page){
	var category = sessionStorage.categoryid ? sessionStorage.categoryid : '';
	var city = sessionStorage.cityid ? sessionStorage.cityid : '';
	var keyword = sessionStorage.keyword ? sessionStorage.keyword : '';
	$.ajax({
		type : 'post',
		url : '/api/v1/merchant/H5MerchantList',
		data : {
			paramStr : "{'category':'" + category + "','city':'" + city + "','keyword':'" + keyword + "','pageIndex':'" + page + "','pageSize':'" + 8 +"'}"
		},
		dataType : 'json',
		timeout : 10000,
		success : function (data) {
			if(data.code == 200){
				var obj = JSON.parse(data.data);
				var shopList = obj.shop_list;
				if(shopList.length != 0){
					var $ul = $('#wrapper ul');
					for(var i = 0; i < shopList.length; i++){
						var $li = $('<li merchantId='+shopList[i]["id"]+'></li>').appendTo($ul);
						$('<img/>').attr('src',shopList[i].imgCover).appendTo($li);
						$('<p></p>').addClass('shopName').text(shopList[i].merchantName).appendTo($li);
						$('<p></p>').addClass('shopIntro').text(shopList[i].traderDesc).appendTo($li);
					}
					$('#wrapper ul li').bind('click',function(){
						sessionStorage.merchantId = $(this).attr('merchantId');
						$('.merchantsBox').css('display','none');
						$('.merchantDetailBox').css('display','block');
						getDetail(sessionStorage.merchantId);
					});
				}else{
					remind('无更多商户');
				}
			}
		},
		error : function (xhr,type,errorThrown) {
			// alert('未知错误');
		}
	});
}

function goBack(){
	window.history.back();
}

function go(){
	$('.merchantDetailBox').css('display','none');
	$('.merchantsBox').css('display','block');
}

function remind(message){
	$('#remindBox').css('display','block').text(message);
	setTimeout(function(){
		$('#remindBox').css('display','none');
	},1500)
}

function hidden(message){
	$('#hiddenBox').css('display','block').text(message);
	setTimeout(function(){
		$('#hiddenBox').css('display','none');
	},1500);
}

function getDetail(id){
	$.ajax({
		type : 'post',
		url : '/api/v1/merchant/H5MerchantDetail',
		dataType : 'json',
		data : {
			paramStr: "{'merchant_id':'" + id + "'}"
		},
		timeout : 10000,
		success : function (data) {
			if(data.code == 200){
				var obj = JSON.parse(data.data);
				var shopDetail = obj.shop_detail;
				$('.merchantDetailBox .top img').attr('src',shopDetail.imgCover);
				$('.merchantDetailBox .p1').text('商户名：' + shopDetail.merchantName);
				$('.merchantDetailBox .p2').text('联系人：' + shopDetail.linkman);
				$('.merchantDetailBox .address').text('地址：' + shopDetail.address);
				$('.merchantDetailBox .telNum').text('电话：' + shopDetail.mobile);
				$('.merchantDetailBox .describe').text('商户描述：' + shopDetail.traderDesc);
				$('.merchantsDetail_title p').text(shopDetail.merchantName);

				$('.merchantDetailBox .merImgCover').attr('src',shopDetail.imgCover);
				if(!shopDetail.showImg1){
					$('.merchantDetailBox .merShowImg1').hide();
				}else{
					$('.merchantDetailBox .merShowImg1').attr('src',shopDetail.showImg1);
				}

				if(!shopDetail.showImg2){
					$('.merchantDetailBox .merShowImg2').hide();
				}else{
					$('.merchantDetailBox .merShowImg2').attr('src',shopDetail.showImg2);
				}

				$('.merchantDetailBox .merPicList img').bind('click',function(){
					sessionStorage.src = $(this).attr('src');
					$('.merchantDetailBox .hiddenBox').show();
					$('.merchantDetailBox .hiddenBox').html('');
					$('<img/>').attr('src',sessionStorage.src).appendTo($('.merchantDetailBox .hiddenBox'));
					var $width = $('.merchantDetailBox .hiddenBox img').width();
					var $height = $('.merchantDetailBox .hiddenBox img').height();
					$('.merchantDetailBox .hiddenBox img').css('width',10 + 'rem').css('height',10/($width/$height) + 'rem');
					$('.merchantDetailBox .hiddenBox').bind('click',function(){
						$('.merchantDetailBox .hiddenBox').hide();
					});
				});
			}
		},
		error : function (xhr,type,errorThrown){

		}
	});
}

var abc = 1;
refresher.init({
	id:"wrapper",
	pullUpAction:Load
});
function Load(){
	abc++;
	getMer(abc);
	wrapper.refresh();
}



$('.chooseCity').click(function(){
	$('.title').text('请选择城市');
	$('.hiddenBox').css('display','block');
	$('#load1').css('display','none');
	$('#load2').css('display','block');
	myScroll2.refresh();
});

$('.category').click(function(){
	$('.title').text('请选择行业分类');
	$('.hiddenBox').css('display','block');
	$('#load2').css('display','none');
	$('#load1').css('display','block');
	myScroll1.refresh();
});

$('.default').click(function(){
	$('#wrapper ul').html('');
	$('.chooseCity a').text('城市');
	$('.category a').text('分类');
	if(sessionStorage.categoryid){
		sessionStorage.removeItem('categoryid');
	}
	if(sessionStorage.cityid){
		sessionStorage.removeItem('cityid');
	}
	if(sessionStorage.keyword){
		sessionStorage.removeItem('keyword');
	}
	getMer(1);
});

$('.searchImg').click(function(){
	var keyword = $('.merchants_title input').val();
	if(!keyword){
		hidden('请输入搜索关键字');
	}else{
		$('#wrapper ul').html('');
		if(sessionStorage.categoryid){
			sessionStorage.removeItem('categoryid');
		}
		if(sessionStorage.cityid){
			sessionStorage.removeItem('cityid');
		}
		sessionStorage.keyword = keyword;
		getMer(1);
	}
});

$('.nav li').click(function(){
	$('.merchants_title input').val('');
});